import { createRouter, createWebHistory } from "vue-router";
import Home from "@/views/Home.vue";
import Login from "@/views/Login.vue";
import Signin from "@/views/Signin.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: "/", component: Home, meta: { requiresAuth: true } },
    { path: "/login", component: Login },
    { path: "/signin", component: Signin },
  ],
});

// 设置导航守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem("authToken"); // 从本地存储获取 Token

  if (to.meta.requiresAuth && !isAuthenticated) {
    // 如果目标路由需要认证，并且用户未认证，则重定向到登录页
    next("/login");
  } else {
    // 否则，允许导航
    next();
  }
});

export default router;
